package cn.qu.phizline.webserver.event.handle.login;

import cn.qu.phizline.event.common.ResponseEvent;
import cn.qu.phizline.event.login.LoginEvent;
import cn.qu.phizline.webserver.event.handle.Handle;
import cn.qu.phizline.webserver.module.user.mapper.UserMapper;
import cn.qu.phizline.webserver.ws.WebServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;

/**
 * Created by zh on 17/8/30.
 */
public class LoginHandle implements Handle<LoginEvent, ResponseEvent> {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CacheManager cacheManager;

    @Override
    public ResponseEvent handle(LoginEvent payload) {
        /*
        LoginEvent.LoginBody body = (LoginEvent.LoginBody) payload.getBody();
        List<User> userList = userMapper.selectByExample(new UserExample() {
            {
                createCriteria()
                        .andUserNameEqualTo(body.getUser())
                        .andUserPwdEqualTo(body.getPasswd());
            }
        });
        String eventKey = WebServer.getPayloadKey(payload.getType(), payload.getVersion());
        if (userList.isEmpty()) {

            return ResponseEvent.build(eventKey, LoginEvent.CODE_NO_USER, null);
        } else {

            return ResponseEvent.build(eventKey, LoginEvent.CODE_LOGIN_SUCCESS, null);
        }
        */

        LoginEvent.LoginBody body = (LoginEvent.LoginBody) payload.getBody();

        String eventKey = WebServer.getPayloadKey(payload.getType(), payload.getVersion());
        return ResponseEvent.build(eventKey, LoginEvent.CODE_LOGIN_SUCCESS, body.getUser());

    }

}
